/**
 * @file 场景控制接口面板实现
 *
 * 场景控制接口面板实现
 *
 * @author wq1977@gmail.com
 */

#include "ScenariosControlPanel.h"
#include "app.h"

/**
  * 场景控制面板的构造函数
  *
  * 场景控制面板包含整个场景窗体的左侧面板.暂时的设计是,最上面为一个文字Logo
  * 中间为场景配置区
  * 下面为场景控制区
  * 最下面为图片Logo
  *
  */ 
ScenariosControlPanel::ScenariosControlPanel(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, const long style)
	:wxPanel(parent,id,pos,size,style){

	//Configure Part.
  wxFlexGridSizer *sizerConf = new wxFlexGridSizer(2);
  sizerConf->AddGrowableCol(1);

	m_Scenario = new wxComboBox(this, -1, _T(""),wxDefaultPosition,
				    wxSize(100,-1),0,NULL,wxCB_READONLY);
	ScPtrArray::iterator iter;
	for (iter=wxGetApp().m_ScenariosManager->AllScenarios.begin();
		 iter!=wxGetApp().m_ScenariosManager->AllScenarios.end();
		 iter++){
		Scenario*p = *iter;
		m_Scenario->Append(p->GetIdentifier(),new wxStringClientData(p->filename));
	}
	
	m_TermNum = new wxTextCtrl(this, SF_TERMNUM, wxString::Format(wxT("%d"),DEFAULT_TERM_NUM),wxDefaultPosition,wxSize(180,-1));
	m_CallTimes = new wxTextCtrl(this, -1, _T("1"));
	m_CallDuration = new wxTextCtrl(this, -1, _T("1"));
	m_CallHold = new wxTextCtrl(this, -1, _T("2"));
	m_CallHeavy = new wxTextCtrl(this, -1, _T("1"));
	
	
	wxStaticBoxSizer *sizertmp =
	  new wxStaticBoxSizer (new wxStaticBox (this, -1, _("Call Control")),
				wxVERTICAL);	
	sizerConf->Add(new wxStaticText(this, -1, _("Terminal Numbers:")), 0,
                  wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
	sizerConf->Add(m_TermNum, 1,
                   wxGROW | wxALIGN_CENTER_VERTICAL);
	sizerConf->Add(new wxStaticText(this, -1, _("Call Times:")), 0,
                  wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
	sizerConf->Add(m_CallTimes, 1,
                   wxGROW | wxALIGN_CENTER_VERTICAL);
	sizerConf->Add(new wxStaticText(this, -1, _("Call Durtion:")), 0,
                  wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
	sizerConf->Add(m_CallDuration, 1,
                   wxGROW | wxALIGN_CENTER_VERTICAL);
	sizerConf->Add(new wxStaticText(this, -1, _("Call Hold:")), 0,
                  wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
	sizerConf->Add(m_CallHold, 1,
                   wxGROW | wxALIGN_CENTER_VERTICAL);
	sizerConf->Add(new wxStaticText(this, -1, _("Call Heavy:")), 0,
                  wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
	sizerConf->Add(m_CallHeavy, 1,
                   wxGROW | wxALIGN_CENTER_VERTICAL);
	sizerConf->Add(new wxStaticText(this, -1, _("Call Scenarios:")), 0,
                  wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
	sizerConf->Add(m_Scenario, 1,
                   wxGROW | wxALIGN_CENTER_VERTICAL);
	sizertmp->Add(sizerConf,1,wxALL | wxEXPAND,5);
	
	//Control Button Part.
	wxFlexGridSizer *sizerBtns = new wxFlexGridSizer(4);
	//wxBoxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL);
	btnCfg= new wxButton(this, CMENU_CONF_SCEN, _("Config"));
	sizerBtns->Add(btnCfg,0,wxALL,5);
	btnStart = new wxButton(this, CMENU_DO_START, _("Start"));
	sizerBtns->Add(btnStart,0,wxALL,5);
	btnStop= new wxButton(this, CMENU_DO_STOP, _("Stop"));
	sizerBtns->Add(btnStop,0,wxALL,5);
	btnReport= new wxButton(this, CMENU_DO_REPORT, _("Report"));
	sizerBtns->Add(btnReport,0,wxALL,5);
	//btnStatic= new wxButton(this, CMENU_DO_REPORT, _("Statistic"));
	//sizerBtns->Add(btnStatic,0,wxALL,5);

	//Control Button Part.
	//wxBoxSizer *sizerchecks = new wxBoxSizer(wxVERTICAL);
	wxFlexGridSizer *sizerchecks = new wxFlexGridSizer(3);
	wxStaticBoxSizer *sizertmpcheck =
	  new wxStaticBoxSizer (new wxStaticBox (this, -1, _("Trace Style")),
				wxVERTICAL);
	m_tracemsg= new wxCheckBox(this, -1, _("Trace Debug"));
	sizerchecks->Add(m_tracemsg,0,wxALL,3);
	m_tracesip= new wxCheckBox(this, -1, _("Trace Sip Message"));
	sizerchecks->Add(m_tracesip,0,wxALL,3);
	m_traceerror= new wxCheckBox(this, -1, _("Trace Error"));
	sizerchecks->Add(m_traceerror,0,wxALL,3);
        sizertmpcheck->Add(sizerchecks,0,wxALL,5);

	wxStaticBoxSizer *sizerset =
	  new wxStaticBoxSizer (new wxStaticBox (this, -1, _("Test Config")),
				wxHORIZONTAL);	
	m_stoponfail= new wxCheckBox(this, -1, _("Stop On Fail"));
	m_waitscefinish= new wxCheckBox(this, -1, _("Safe Stop"));
	m_waitscefinish->SetValue(true);
	sizerset->Add(m_stoponfail,0,wxALL,5);
	sizerset->Add(m_waitscefinish,0,wxALL,5);


	wxString choices[] =
	  {
	    _("Fixed Case Test"),
	    _("Round Case Test"),
	    _("Random Case Test"),
	  };

	
	m_teststyle = new wxRadioBox(this, CMENU_CASE_STYLE, _("Test Style"), 
				   wxDefaultPosition, wxDefaultSize,
				     3,choices,1,wxRA_SPECIFY_ROWS);

	wxString finalchoices[] =
	  {
	    _("Never Execute"),
	    _("Scenario Finish"),
	    _("Case Finish"),
	  };

	
	m_finalRule = new wxRadioBox(this, -1, _("Final Rule Execute when"), 
				   wxDefaultPosition, wxDefaultSize,
				     3,finalchoices,1,wxRA_SPECIFY_ROWS);


	/*wxStaticBoxSizer *sizerreports =
	  new wxStaticBoxSizer (new wxStaticBox (this, -1, _("Report Style")),
				wxVERTICAL);	
	m_reportonend= new wxCheckBox(this, -1, _("Report On End"));
	sizerreports->Add(m_reportonend,0,wxALL,5);*/

	
	wxBoxSizer *sizertmp2 = new wxBoxSizer(wxHORIZONTAL);

	//Now Add All Those Part.
	wxSizer *sizerV = new wxBoxSizer(wxVERTICAL);
	sizerV->Add(sizertmp, 0, wxALL | wxEXPAND, 5);
	sizerV->Add(m_teststyle, 0, wxALL | wxEXPAND , 5);
	sizerV->Add(m_finalRule, 0, wxALL | wxEXPAND , 5);
	sizerV->Add(sizerset, 0, wxALL | wxEXPAND , 5);
	//sizerV->Add(sizerreports,0,wxALL | wxEXPAND , 5);
	sizerV->Add(sizertmpcheck, 0, wxALL | wxEXPAND , 0);
	sizerV->Add(sizertmp2,1,0,0);
	sizerV->Add(sizerBtns, 0, wxALL, 0);
	SetAutoLayout(TRUE);
	SetSizer(sizerV);

	btnStop->Enable(false);

	wxSize besrSize = GetBestSize();
	SetSize(besrSize);

}

ScenariosControlPanel::~ScenariosControlPanel(){
}

int ScenariosControlPanel::GetTermNum(){
	long v=-1;
	if (m_TermNum->GetValue().ToLong(&v)){
		return v;
	}
	return -1;
}
int ScenariosControlPanel::GetCallTimes(){
	long v=-1;
	if (m_CallTimes->GetValue().ToLong(&v)){
		return v;
	}
	return -1;
}
int ScenariosControlPanel::GetCallDuration(){
	long v=-1;
	if (m_CallDuration->GetValue().ToLong(&v)){
		return v;
	}
	return -1;
}
int ScenariosControlPanel::GetCallHold(){
	long v=-1;
	if (m_CallHold->GetValue().ToLong(&v)){
		return v;
	}
	return -1;
}

int ScenariosControlPanel::GetHeavy(){
	long v=-1;
	if (m_CallHeavy->GetValue().ToLong(&v)){
		return v;
	}
	return -1;
}

void ScenariosControlPanel::LockControl(bool unlock){
	m_Scenario->Enable(false);
	m_TermNum->Enable(unlock);
	m_CallTimes->Enable(unlock);
	m_CallDuration->Enable(unlock);
	m_CallHold->Enable(unlock);
	m_CallHeavy->Enable(unlock);
	btnStart->Enable(unlock);
	btnStop->Enable(!unlock);
	/*btnReport->Enable(false);
	ScenariosForm *Handle= (ScenariosForm*)GetParent();
	if (Handle){
	  if ((!Handle->LastReportPath.IsEmpty()) || (Handle->GetScenarioControl())){
	    btnReport->Enable(true);
	  }
	  }*/
	btnCfg->Enable(unlock);
	m_teststyle->Enable(unlock);
	if ((unlock) &&(m_teststyle->GetSelection()==0)){
	  m_Scenario->Enable(true);
	}
}

